package com.example.memo_app.acties;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.provider.AlarmClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.bumptech.glide.Glide;
import com.example.memo_app.R;
import com.example.memo_app.dao.MemoBean;
import com.xuexiang.xui.widget.button.ButtonView;

import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * 备忘录详情页面
 */
public class DetailActivity extends AppCompatActivity {

    List<String> paths = new ArrayList<>();
    private MemoBean memoBean;

    private TextView mTvTime;
    private TextView mTvTitle;
    private TextView mTvContent;
    private RecyclerView mIvFirst;
    private ButtonView mBtnDeleteClock;
    private ButtonView mBtnAddClock;
    private TextView mTvClock;
    private SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        memoBean = (MemoBean) getIntent().getSerializableExtra("data");
        paths.addAll(memoBean.getPath());

        mTvTime = findViewById(R.id.tv_time);
        mTvTitle = findViewById(R.id.tv_title);
        mTvContent = findViewById(R.id.tv_content);
        mIvFirst = findViewById(R.id.iv_first);

        mTvClock = findViewById(R.id.tv_clock);

        mBtnDeleteClock = findViewById(R.id.btn_delete_clock);
        mBtnAddClock = findViewById(R.id.btn_add_clock);

        mIvFirst.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        mIvFirst.setAdapter(new MyPicAdapter());
        mTvTime.setText(memoBean.getCreatedAt());
        mTvContent.setText(memoBean.getMessage());
        mTvTitle.setText(memoBean.getTitle());

        long date = memoBean.getDate();
        if (date == 0) {
            mTvClock.setText("还未添加闹钟");
        } else {
            mTvClock.setText("闹钟时间:" + format.format(new Date(memoBean.getDate())));
        }

        mBtnDeleteClock.setOnClickListener(v -> {
            mTvClock.setText("还未添加闹钟");
            memoBean.setDate(0);
            memoBean.update(new UpdateListener() {
                @Override
                public void done(BmobException e) {

                }
            });
            //  dismissAlarm(memoBean.getTitle());
        });

        mBtnAddClock.setOnClickListener(v -> {
            //显示日期选择对话框
            showDateDialog();
        });

    }

    private void showDateDialog() {
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        //startDate.set(2013,1,1);
        Calendar endDate = Calendar.getInstance();
        //endDate.set(2020,1,1);
        startDate.set(startDate.get(Calendar.YEAR), startDate.get(Calendar.MONTH), startDate.get(Calendar.DATE));
        endDate.set(2030, 0, 1);

        TimePickerView pvTime = new TimePickerBuilder(this, (date, v) -> {//选中事件回调
            mTvClock.setText("闹钟时间:" + format.format(date));
            memoBean.setDate(date.getTime());
            memoBean.update(new UpdateListener() {
                @Override
                public void done(BmobException e) {

                }
            });
            //设置闹钟
            setClock(date);
        })
                .setType(new boolean[]{true, true, true, true, true, true})// 默认全部显示
                .setCancelText("取消")//取消按钮文字
                .setSubmitText("确认")//确认按钮文字
                .setContentTextSize(18)//滚轮文字大小
                .setTitleSize(20)//标题文字大小
                .setTitleText("闹钟时间选择")//标题文字
                .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(true)//是否循环滚动
                .setTitleColor(Color.BLACK)//标题文字颜色
                .setSubmitColor(Color.BLUE)//确定按钮文字颜色
                .setCancelColor(Color.BLUE)//取消按钮文字颜色
                .setTitleBgColor(0xFF666666)//标题背景颜色 Night mode
                .setBgColor(0xFF333333)//滚轮背景颜色 Night mode
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate, endDate)//起始终止年月日设定
                .setLabel("年", "月", "日", "时", "分", "秒")//默认设置为年月日时分秒
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .isDialog(true)//是否显示为对话框样式
                .build();
        pvTime.show();

    }


    //设置闹钟
    private void setClock(Date date) {
        int hours = date.getHours();
        int minutes = date.getMinutes();
    }


    class MyPicAdapter extends RecyclerView.Adapter<MyPicViewHolder> {

        @NonNull
        @NotNull
        @Override
        public MyPicViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(DetailActivity.this).inflate(R.layout.item_iamgeview2, parent, false);
            return new MyPicViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull @NotNull MyPicViewHolder holder, int position) {
            //显示图片
            Glide.with(DetailActivity.this).load(paths.get(position)).into(holder.view);
            holder.view.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    paths.remove(position);
                    notifyDataSetChanged();
                    return true;
                }
            });
        }

        @Override
        public int getItemCount() {
            return paths.size();
        }
    }


    static class MyPicViewHolder extends RecyclerView.ViewHolder {

        ImageView view;

        public MyPicViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            view = (ImageView) itemView;
        }
    }
}